﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Windows.Forms;
using OpenCvSharp;
using OpenCvSharp.CPlusPlus;
using System.IO;

namespace Сlassifier_Tool_For_OpenCV
{
    public partial class TestHOG : Form
    {
        public TestHOG()
        {
            InitializeComponent();
        }

        CvSize _winSize;
        CvSize _blockSize;
        CvSize _blockStride;
        CvSize _cellSize;
         double _hit;
        int _nbins;
        int _aperature;
        int _winSigma;

        bool SetSetting()
        {
            try
            {
                _winSize = new CvSize(Convert.ToInt32(textBox3.Text), Convert.ToInt32(textBox4.Text));
                _blockSize = new CvSize(Convert.ToInt32(textBox6.Text), Convert.ToInt32(textBox5.Text));
                _blockStride = new CvSize(Convert.ToInt32(textBox8.Text), Convert.ToInt32(textBox7.Text));
                _cellSize = new CvSize(Convert.ToInt32(textBox10.Text), Convert.ToInt32(textBox9.Text));
                _nbins = Convert.ToInt32(textBox11.Text);
                _aperature = Convert.ToInt32(textBox12.Text);
                _winSigma = Convert.ToInt32(textBox13.Text);
                _hit = Convert.ToDouble(textBox20.Text, CultureInfo.InvariantCulture);
                return true;
            }
            catch
            {
                return false;
            }
        }

        private void Test_HOG_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (openFileDialog2.ShowDialog() == DialogResult.OK)
                textBox1.Text = openFileDialog2.FileName;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (File.Exists(textBox1.Text))
            {
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    if (SetSetting())
                    {
                        var s = File.ReadAllText(textBox1.Text).Split(' ');
                        var d = new float[s.Length];
                        for (var i = 0; i < s.Length; i++)
                            d[i] = (float)Convert.ToDouble(s[i], CultureInfo.InvariantCulture);
                        var hog = new HOGDescriptor(_winSize, _blockSize, _blockStride, _cellSize, _nbins, _aperature, _winSigma);
                        hog.SetSVMDetector(d);
                        var im = new IplImage(openFileDialog1.FileName, LoadMode.GrayScale);

                        var r = hog.DetectMultiScale(new Mat(im),_hit, new CvSize(Convert.ToInt32(textBox19.Text), Convert.ToInt32(textBox18.Text))
                            , new CvSize(Convert.ToInt32(textBox17.Text), Convert.ToInt32(textBox16.Text)), Convert.ToDouble(textBox15.Text));

                        foreach (var rr in r)
                            im.DrawRect(rr, CvColor.Orange);
                        var ts = Guid.NewGuid();
                        im.SaveImage(ts + ".jpeg");
                        pictureBox1.Image = new System.Drawing.Bitmap(ts + ".jpeg");
                    }
                    else
                        MessageBox.Show("incorrect setting");
                }
            }
            else MessageBox.Show("non select vector file");
        }

        IEnumerable<float> HOG(string file)
        {
            var im = new IplImage(file, LoadMode.GrayScale);
            var image = new IplImage(_winSize, im.Depth, im.NChannels);
            Cv.Resize(im, image);
            var hog = new HOGDescriptor(_winSize, _blockSize, _blockStride, _cellSize, _nbins, _aperature, _winSigma);
            var m = new Mat(image);
            var f = hog.Compute(m);
            image.Dispose();
            m.Dispose();
            im.Dispose();
            return f;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (File.Exists(textBox1.Text))
            {
                if (  openFileDialog3.ShowDialog() == DialogResult.OK)
                {
                    if (SetSetting())
                    {
                        var s = File.ReadAllText(textBox1.Text).Split(' ');
                        var d = new float[s.Length];
                             for (var i = 0; i < s.Length; i++)
                                 d[i] = (float)Convert.ToDouble(s[i], CultureInfo.InvariantCulture);
                        foreach (var f in openFileDialog3.FileNames)
                        {
                            var h = HOG(f);
                            float m= h.Select((t, i) => t*d[i]).Sum();
                            if (!Directory.Exists("positiv")) Directory.CreateDirectory("positiv");
                            if (!Directory.Exists("negotiv")) Directory.CreateDirectory("negotiv");
                            if (!checkBox1.Checked)
                            {
                                if (m + d[d.Length - 1] > _hit)
                                    File.Copy(f, "positiv\\" + Path.GetFileName(f), true);
                                else
                                    File.Copy(f, "negotiv\\" + Path.GetFileName(f), true);
                            }
                            else
                            {
                                var im = new IplImage(f);
                                if (m + d[d.Length - 1] > _hit)
                                    im.SaveImage("positiv\\" + Path.GetFileNameWithoutExtension(f) + ".jpg");
                                else
                                    im.SaveImage("negotiv\\" + Path.GetFileNameWithoutExtension(f) + ".jpg");
                              
                                im.Dispose();
                            }
                        }
                    }
                    else
                        MessageBox.Show("incorrect setting");
                }
            }
            else MessageBox.Show("non select vector file");

        }

        private void button4_Click(object sender, EventArgs e)
        {

        }
    }
}
